package cn.itcast.interceptcall;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.mylhyl.acp.Acp;
import com.mylhyl.acp.AcpListener;
import com.mylhyl.acp.AcpOptions;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private EditText et_ipnumber;
    private SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_ipnumber = (EditText) findViewById(R.id.et_ipnumber);
        // 创建SharedPreferences对象
        sp = getSharedPreferences("config", MODE_PRIVATE);


        //动态申请权限
        //权限分类 https://blog.csdn.net/u011277123/article/details/52192732
        Acp.getInstance(this).request(new AcpOptions.Builder().setPermissions(
                Manifest.permission.PROCESS_OUTGOING_CALLS,Manifest.permission.CALL_PHONE).build(), new AcpListener() {
            @Override
            public void onGranted() {
                Toast.makeText(MainActivity.this,"PROCESS_OUTGOING_CALLS权限已获取",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDenied(List<String> permissions) {
                Toast.makeText(MainActivity.this,"PROCESS_OUTGOING_CALLS权限已拒绝",Toast.LENGTH_SHORT).show();
            }
        });
    }


    public void click(View view) {
        // 获取用户输入的拦截号码
        String number = et_ipnumber.getText().toString().trim();
        saveData(number);
    }

    private void saveData(String number){
        //创建Editor对象,保存用户输入的拦截号码
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("number", number);
        editor.commit();
        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
    }


}
